import {getStorage, removeStorage, setStorage} from '../utils';

// export const apiUserUrl = process.env.NODE_ENV === 'production' ? 'http://server.dev.intetex.com:8500/mainuser' : 'http://server.dev.intetex.com:8500/mainuser'
export const apiBaseUrl = process.env.NODE_ENV === 'production' ? 'http://server.dev.intetex.com:8001/erpcompany' : 'http://server.dev.intetex.com:8001/erpcompany';

export const urlHttp = 'http://';

export const defaultSetting = {
  showSettings: false,
  tagsView: true,
  fixedHeader: false,
  sidebarLogo: true,
  errorLog: 'production',
  fixedTags: true,
  menuStyle: 'light',
  theme: '#1492FF'
};

const settingKey = 'app-settings';
export function getSettings (key) {
  let data = getStorage(settingKey);
  if (!data) {
    if (key !== undefined && key && defaultSetting[key] !== undefined) {
      return defaultSetting[key];
    } else {
      return defaultSetting;
    }
  }
  try {
    let json = JSON.parse(data);
    for (let i in defaultSetting) {
      if (json[i] === undefined) {
        json[i] = defaultSetting[i];
      }
    }
    if (key !== undefined && json[key] !== undefined) {
      return json[key];
    }
    return json;
  } catch (e) {
    console.log(e);
    return defaultSetting;
  }
}

export function setSettings (obj) {
  let settings = getSettings();
  if (settings) {
    settings = JSON.parse(JSON.stringify(settings));
  }
  if (!settings) {
    settings = {};
  }
  for (let i in obj) {
    let value = obj[i];
    if (value === false) {
      value = 0;
    } else if (value === true) {
      value = 1;
    }
    settings[i] = value;
  }
  let string = JSON.stringify(settings);
  setStorage(settingKey, string);
}

export function removeSettings () {
  removeStorage(settingKey);
}
